const constants = require('../../../common/Constants.js');
const network_utils = require('../../../http/NetworkUtils.js');
import * as umfToast from '../../../utils/UmfToast';
import imPayCommon from '../openPos/feeRateInfo/imPayCommon/imPayCommon';
import { $Toptips } from '../../../components/CompRouter';

var MAX_CODE = 4;//收款码上限

const PageStates = {
    //正常：开通立马付产品
    normal: {isDesploy: false, rateDisable: false, codeDisable: false, completeBack: false},
    //展示：部署详情
    desploy: {isDesploy: true, rateDisable: false, codeDisable: false, completeBack: true},
    //展示费率不可修修改：店铺信息
    desployRateDisable: {isDesploy: true, rateDisable: true, codeDisable: false, completeBack: true},
    //展示所有不可修改：查询商户
    desployAllDisable: {isDesploy: true, rateDisable: true, codeDisable: true, completeBack: true},
};

Page(Object.assign({
    data: {
        storeId: '',
        qrCodeList: [{index: 0, code: '', active: true}],

        pageState: PageStates.normal
    },

    onLoad: function (options) {
        let pageState = PageStates[options.pageState];

        this.setData({
            storeId: options.storeId,
            pageState: pageState,
        });

        if (pageState.isDesploy) {
            //展示的场合，需要查询费率及收款码
            this._initData(options.storeId);
        }

    },


    /**
     * 详情跳转过来请求各个参数
     */
    _initData(storeId) {
        let params = {
            storeId: storeId
        };
        network_utils.buildRequest(constants.Interfaces.FUNC_IMPAY_DISPLAY, params, {
            doHttpSucess: (response) => {
                if (constants.RetCodes.SUCCESS === response.retCode) {
                    this.setData({
                        alipayRate: response.alipayRate,
                        wechatRate: response.wechatRate,
                        qrCodeList: this._getCodeList(response.qrCodeList)
                    });
                    return true;
                }
                return false;
            }
        }).send();
    },

    /**
     * 下一步
     */
    nextStep: function (ee) {
        let wechatRate = ee.detail.value.wechatRate;
        let alipayRate = ee.detail.value.alipayRate;
        let errMsg = this._imCheckForm(wechatRate, alipayRate)
        if (errMsg) {
          $Toptips.error({ text: errMsg });
        }else {
          this._requestOpenImPay(wechatRate, alipayRate);
        }
    },

    /**
     * 请求开通立马付
     */
    _requestOpenImPay: function (wechatRate, alipayRate) {
        let params = {
            storeId: this.data.storeId,
            wechatRate: wechatRate,
            alipayRate: alipayRate,
            qrCodeList: this.RemoveDuplicate(this.convertCodeList(this.data.qrCodeList))
        };
        network_utils.buildRequest(constants.Interfaces.FUNC_OPEN_IMPAY, params, {
            doHttpSucess: (response) => {
                if (constants.RetCodes.SUCCESS === response.retCode) {
                    this._onRequestSuccess(response)
                    return true;
                }
                return false;
            }
        }).send();
    },

    /**
     * 区分情况跳转
     */
    _onRequestSuccess(result) {
        if (this.data.pageState.completeBack) {
            umfToast.showToast("操作成功");
            setTimeout(() => {
                wx.navigateBack({delta: 1});
            }, 1000);
            return;
        }

        let jsonData = {
            storeId: result["storeId"] ? result["storeId"] : "",
            storeShortName: result["storeShortName"] ? result["storeShortName"] : "",
            payeeName: result["payeeName"] ? result["payeeName"] : "",
            qrUrl: result["qrCodeUrl"] ? encodeURIComponent(result["qrCodeUrl"]) : ""
        };
        wx.redirectTo({
            url: '../inviteMerchants/inviteMerchants?jsonData=' + JSON.stringify(jsonData),
        });
    },

    

    /**
     * 详情跳转过来，收款码数组初始化
     */
    _getCodeList(list) {
        if (!list || list.length === 0) {
            return [{index: 0, code: '', active: true}];
        }

        let codeList = [];
        for (let i = 0; i < list.length; i++) {
            codeList.push({index: i, code: list[i], active: false});
        }

        if (codeList.length < MAX_CODE) {
            codeList.push({index: list.length, code: '', active: true});
        }
        return codeList;
    }
},imPayCommon));

